package com.tepth.remote.control.delegate.apply.create;

import com.tepth.remote.control.base.config.ApiDictionary;
import com.tepth.remote.control.manager.ConfigManager;
import com.tepth.remote.core.net.CommonParams;
import com.tepth.remote.core.net.callback.IError;
import com.tepth.remote.core.net.callback.ISoapSuccess;

import org.jetbrains.annotations.NotNull;
import org.ksoap2.serialization.SoapObject;

import java.util.WeakHashMap;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/24
 */
public class ApplyCreatePresenter extends ApplyCreateContract.AbstractPresenter {
    private final ApplyCreateContract.View mView;
    private final ApplyCreateContract.Model mModel;

    public ApplyCreatePresenter(@NotNull ApplyCreateContract.View view) {
        mView = view;
        mModel = new ApplyCreateModel();
    }

    @Override
    void getExamination() {
        mView.showLoading("正在加载");
        mModel.onApply(new WeakHashMap<String, Object>(), ApiDictionary.API_GET_EXCEPTION_LIST, ApiDictionary.SOAP_NAME_SPACE, new ISoapSuccess() {
            @Override
            public void onSuccess(SoapObject object) {
                final int code = Integer.parseInt(object.getProperty("Code").toString());
                if (code == CommonParams.SUCCESS_CODE_NORMAL) {
                    final SoapObject value = (SoapObject) object
                            .getProperty("Value");
                    final int size = value.getPropertyCount();
                    final WeakHashMap<String, String> result = new WeakHashMap<>();
                    for (int i = 0; i < size; i++) {
                        final SoapObject soapObject = (SoapObject) value
                                .getProperty(i);
                        result.put(soapObject.getPropertySafelyAsString("Auto_ID"),
                                soapObject.getPropertySafelyAsString("EC_Name"));
                    }
                    mView.getExaminationSuccess(result);
                } else {
                    mView.getExaminationFailed(code, "获取请假配置信息失败");
                }
            }
        }, new IError() {
            @Override
            public void onError(int code, String msg) {
                mView.getExaminationFailed(code, msg);
            }
        });
    }

    @Override
    void submitApply(int projectId, String examinationId, String startDate, String endDate, String reason) {
        mView.showLoading("正在提交");
        final WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("applyUid", ConfigManager.getInstance().getUid());
        params.put("applyType", examinationId);
        params.put("xid", projectId);
        params.put("beginDate", startDate);
        params.put("endDate", endDate);
        params.put("reason", reason);
        mModel.onApply(params, "ApplyException", ApiDictionary.SOAP_NAME_SPACE, new ISoapSuccess() {
            @Override
            public void onSuccess(SoapObject object) {
                mView.hideLoading();
                final int code = Integer.parseInt(object.getProperty("Code").toString());
                if (code == CommonParams.SUCCESS_CODE_NORMAL) {
                    mView.submitApplySuccess();
                } else {
                    mView.submitApplyFailed(code, "提交失败");
                }
            }
        }, new IError() {
            @Override
            public void onError(int code, String msg) {
                mView.submitApplyFailed(code, msg);
            }
        });
    }
}
